import datetime
import os
import time
from time import sleep

from config.driver_config import DriverConfig
from contains import IMAGE_PATH
from element_handlers.action_handlers import ActionHandlers
from element_handlers.element_handlers import ElementHandlers
from utils.handle_cookies import save_cookies, load_cookies
from xpath_handlers.weibo import WeiboXpath


class LoginWeibo(WeiboXpath, ElementHandlers):

    def login(self):
        login_url = 'https://weibo.com/login.php'

        driver = DriverConfig().driver_config()

        self.element_to_url(driver, login_url)
        self.element_click(driver, self.login_phone_tab())

        self.element_fill_value(driver, self.input_phone(), '18621374967')
        self.element_click(driver, self.send_verification_code())
        data = input('输入短信验证码：')
        self.element_fill_value(driver, self.input_verification_code(), data)

        self.element_click(driver, self.login_button())

        self.wait_for_ready_state_complete(driver)

        self.wait_login_success(driver, self.login_success())

        weibo_cookies = driver.get_cookies()

        save_cookies(weibo_cookies, 'weibo')
        sleep(2)

        driver.quit()


class LoginWeiboWithCookies(WeiboXpath, ElementHandlers, ActionHandlers):
    def login(self):
        print(datetime.datetime.now())
        driver = DriverConfig().driver_config()
        home_url = 'https://weibo.com/'

        self.element_to_url(driver, home_url)

        print(driver.current_url)

        is_home = False
        if driver.current_url == 'https://weibo.com/overseas':
            while is_home is False:
                is_home = self.is_element_display(driver, self.oversea_home_sign())
        else:
            while is_home is False:
                is_home = self.is_element_display(driver, self.home_sign())

        driver.delete_all_cookies()
        # sleep(1)
        cookies = load_cookies('weibo')

        for cookie in cookies:
            cookie_dict = {
                'domain': '.weibo.com',
                'name': cookie.get('name'),
                'value': cookie.get('value'),
                "expires": '',
                'path': '/',
                'httpOnly': False,
                'HostOnly': False,
                'Secure': False
            }
            driver.add_cookie(cookie_dict)

        # sleep(1)
        driver.refresh()

        self.element_to_url(driver, home_url)
        print(datetime.datetime.now())

        sleep(2)
        # self.send_normal_blog(driver)
        # self.send_image_blog(driver)
        self.send_timer_blog(driver)
        sleep(100)

        driver.quit()

    def send_normal_blog(self, driver):
        content = "This is a blog create by #selenium#"
        self.element_fill_value(driver, self.blog_content(), content)
        self.element_click(driver, self.blog_send())

    def send_image_blog(self, driver):
        content = "This is a image blog create by #selenium#"
        self.element_fill_value(driver, self.blog_content(), content)
        for image in ['Lee.png', '怪兽.jpeg', '皮卡丘.jpeg']:
            image_path = os.path.join(IMAGE_PATH, image)
            self.upload(driver, self.blog_image(), image_path)
        self.element_click(driver, self.blog_send())

    def send_timer_blog(self, driver):
        content = "This is a timer blog create by selenium"
        self.element_fill_value(driver, self.blog_content(), content)

        self.action_click(driver, self.element_get(driver, self.blog_timer_btn()))
        self.element_fill_value(driver, self.blog_timer_date(), time.strftime("%Y-%m-%d", time.localtime()))

        self.action_click(driver, self.element_get(driver, self.blog_timer_hour_btn()))
        hour = str(int(time.strftime("%H", time.localtime())) + 1)
        self.element_click(driver, self.choose_blog_timer_hour(hour))

        self.action_click(driver, self.element_get(driver, self.blog_timer_minute_btn()))
        self.element_click(driver, self.choose_blog_timer_minute('0'))

        self.auto_choose_blog_type(driver, '仅自己可见')

        self.element_click(driver, self.blog_send())

    def auto_choose_blog_type(self, driver, type_name):
        self.action_click_xpath(driver, self.blog_type_btn())
        self.element_click(driver, self.choose_blog_type(type_name))


if __name__ == '__main__':
    LoginWeiboWithCookies().login()
